<template>
	<view class="container" :style="{'overflow':showPoster?'hidden':'scroll'}">
		<poster   v-model:show="showPoster" :topText="topText" :img="posterImg" :lastText="lastText"  />
		<view class="position-relative">
			<template v-for="(item, index) in globalState.mineImgs" :key="index">
				<image :src="item.url"  class="bg"  v-if="item.code == 1002" />
			</template>
		</view>
		<view style="padding: 0 30rpx">
			<!-- user box begin -->
			<view class="d-flex flex-column bg-white user-box">
				<view class="d-flex align-items-center">
					<view class="avatar">
						<image :src="userState.avatar ? userState.avatar : '/static/images/mine/default.png'" @tap="writeName"></image>
						<view class="badge" v-if="isLogin">
							<image src="/static/images/mine/level.png"></image>
							<view>{{ member.memberLevel }}</view>
						</view>
					</view>
					<view class="d-flex flex-column flex-fill overflow-hidden" style="margin-top: 20rpx">
						<view class="font-size-lg font-weight-bold" @tap="writeName">{{ userState.nickname ? userState.nickname : '点击修改头像昵称' }}</view>
					</view>
				</view>
				<!-- user grid begin -->
				<view class="w-100 d-flex align-items-center just-content-center" v-show="userState.role == ROLE_MER">
					<view class="user-grid">
						<view class="value font-size-extra-lg font-weight-bold text-color-base">{{ (userState.store_td_cash / 100).toFixed(2) }}</view>
						<view class="font-size-sm text-color-assist">今日收款</view>
					</view>
					<view class="user-grid">
						<view class="value font-size-extra-lg font-weight-bold text-color-base">{{ (userState.store_can_withdrawal / 100).toFixed(2) }}</view>
						<view class="font-size-sm text-color-assist">可提现</view>
					</view>
					<view class="user-grid">
						<view class="value font-size-extra-lg font-weight-bold text-color-base">{{ userState.store_td_orders }}</view>
						<view class="font-size-sm text-color-assist">今日单量</view>
					</view>
				</view>
				<!-- user grid end -->
			</view>
		
			<!-- banner begin -->
			<view style="height:19px" v-show="userState.role == ROLE_USER" />
			<template v-for="(item, index) in globalState.mineImgs" :key="index">
				<image :src="item.url"  class="banner" mode="widthFix" v-if="item.code == 1001" />
			</template>
			
	<!-- 		<image src="/static/images/mine/banner.png" class="banner" mode="widthFix"></image> -->
			<!-- bennaer end -->
		</view>
		<!-- service box begin -->
		<view class="service-box">
			<view class="font-size-lg text-color-base font-weight-bold" style="margin-bottom: 20rpx">我的服务</view>
			<view class="row">
				<view class="grid" @tap="changeToUser" v-if="userState.store_phone">
					<view class="image">
						<image src="/static/images/mine/exchange.png"></image>
					</view>
					<view>切换 {{ userState.role == ROLE_USER ? '商家端' : '用户端' }}</view>
				</view>
				<template v-if="userState.role == ROLE_MER">
					<!--商户功能-->
					<view class="grid" @tap="nav('/pages/merchant/goods')">
						<view class="image">
							<image src="/static/images/mine/file.png"></image>
						</view>
						<view>商品管理</view>
					</view>
					<view class="grid" @tap="nav('/pages/merchant/category_op')">
						<view class="image">
							<image src="/static/images/mine/wdzl.png"></image>
						</view>
						<view>分类管理</view>
					</view>
					<view class="grid" @tap="orders">
						<view class="image">
							<image src="/static/images/mine/order.png"></image>
						</view>
						<view>我的订单</view>
					</view>
					<view class="grid" @tap="shareCode">
						<view class="image">
							<image src="/static/images/mine/code.png"></image>
						</view>
						<view>店铺码</view>
					</view>
					<view class="grid" @tap="applyCash">
						<view class="image">
							<image src="/static/images/mine/coin.png"></image>
						</view>
						<view>申请提现</view>
					</view>
					<view class="grid" @tap="withdrawRecord">
						<view class="image">
							<image src="/static/images/mine/file2.png"></image>
						</view>
						<view>提现记录</view>
					</view>
					<view class="grid" @click="merEnter">
						<view class="image">
							<image src="/static/images/mine/file3.png"></image>
						</view>
						<view>商户资料</view>
					</view>
				</template>
				<template v-else-if="!sysState.hide_mer">
					<!--用户功能-->
					<view class="grid" @click="merEnter" v-if="userState.store_phone == ''">
						<view class="image">
							<image src="/static/images/mine/jfqd.png"></image>
						</view>
						<view>商家入驻</view>
					</view>
					<view class="grid" @tap="orders">
						<view class="image">
							<image src="/static/images/mine/order.png"></image>
						</view>
						<view>我的订单</view>
					</view>
					<view class="grid" @tap="addresses">
						<view class="image">
							<image src="/static/images/mine/shdz.png"></image>
						</view>
						<view>收货地址</view>
					</view>
				</template>
		
				<view class="grid">
					<view class="image">
						<image src="/static/images/mine/lxkf.png"></image>
					</view>
					<view>
						<button open-type="contact" class="contact" />
						联系客服
					</view>
				</view>
			</view>
		</view>
		<!-- service box end -->
		<!-- tips begin -->
		<view class="d-flex just-content-center align-items-center text-color-assist" style="padding: 30rpx 0; font-size: 22rpx">美好的事情终会发生~</view>
		<!-- tisps end -->
	</view>
</template>

<script lang="ts" setup>
import { ref } from 'vue';

import poster from '../components/poster.vue';
import { setChangeRole, state as userState } from '../../store/user';
import { state as sysState } from '../../store/system';
import { ROLE_USER, ROLE_MER } from '../../store/user';
import getUser from '../../vendor/user';
import  {state as globalState} from '@/store/global'
const { applyWithdrawal } = getUser();

 
const isLogin = ref(false);
const showPoster = ref(false);
const topText = ref('Hi,我在邻居优选开了一家店铺'); // 上标题



const posterImg = ref('') // 中心图片
const img  = globalState.mineImgs.find((item)=>item.code == 1003)
if(img){
	posterImg.value = img.url
}
const lastText = ref('物美价廉,当天送达,快扫码体验吧~');
// const price = 1200;
function writeName() {
	// 修改用户名 和头像
	uni.navigateTo({
		url: '/pages/user/profile'
	});
}

const changeToUser = () => {
	if(!userState.store_phone ){
		uni.showToast({
			title: '未入驻,请先选择商家入驻',
			icon:"none"
		});
		return 
	}
	
	setChangeRole();
};

const shareCode = () => {
	console.log(userState)
	showPoster.value = true;
};

const applyCash = () => {
	if (userState.store_can_withdrawal  < 50  ) {
		uni.showToast({
			title: '金额小于0.5元无法发起提现',
			icon:"none"
		});
		return;
	}
	
 
	applyWithdrawal((res:any) => {
		console.log(res)
		if(res.msg != "成功"){
			uni.showToast({
				title: res.msg,
				icon:"none"
			});
		}else{
			uni.showToast({
				title: '提现发起成功'
			});
			userState.store_can_withdrawal = 0 
		}
		
	});
};

const merEnter = () => {
	uni.navigateTo({
		url: '/pages/merchant/profile'
	});
};

const withdrawRecord = () => {
	uni.navigateTo({
		url: '/pages/merchant/wd_record'
	});
};

 
const orders = () => {
	uni.switchTab({
		url: '/pages/tabbar/orders'
	});
};

const addresses = () => {
	uni.navigateTo({
		url: '/pages/user/address'
	});
};

const nav = (url:string)=>{
	uni.navigateTo({
		url
	});
}

const save = () => {};
</script>

<style lang="scss" scoped>
.grid {
	position: relative;
}
page {
	height: auto;
	min-height: 100%;
}

.bg {
	width: 100%;
	height: calc(410 / 594 * 750rpx);
}

.hym-btn {
	position: absolute;
	top: 40rpx;
	right: 40rpx;
	color: $color-primary;
	display: flex;
	align-items: center;
	justify-content: center;
	border-radius: 50rem;
	font-size: $font-size-sm;
	box-shadow: 0 0 20rpx rgba(66, 66, 66, 0.1);
	&::after {
		border: 0;
	}

	image {
		width: 30rpx;
		height: 30rpx;
		margin-right: 10rpx;
	}
}

.user-box {
	position: relative;
	border-radius: 8rpx;
	margin-bottom: 30rpx;
	margin-top: -115rpx;
	box-shadow: $box-shadow;
}

.avatar {
	position: relative;
	margin-top: -35rpx;
	margin-left: 35rpx;
	margin-right: 35rpx;
	width: 160rpx;
	height: 160rpx;
	border-radius: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	background-color: #ffffff;
	box-shadow: 0 0 20rpx rgba($color: #000000, $alpha: 0.2);

	image {
		width: 140rpx;
		height: 140rpx;
		border-radius: 100%;
	}

	.badge {
		position: absolute;
		right: -10rpx;
		bottom: -10rpx;
		background-color: #ffffff;
		border-radius: 50rem;
		display: flex;
		align-items: center;
		justify-content: center;
		color: $color-warning;
		font-size: 24rpx;
		padding: 8rpx 16rpx;
		box-shadow: 0 0 20rpx rgba($color: #000000, $alpha: 0.2);

		image {
			width: 30rpx;
			height: 30rpx;
		}
	}
}

.level-benefit {
	margin-left: 35rpx;
	padding: 10rpx 0 10rpx 30rpx;
	border-radius: 50rem 0 0 50rem;
}

.user-grid {
	width: 25%;
	padding: 30rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;

	.value {
		margin-bottom: 20rpx;
	}
}

.level-benefit-box {
	border-radius: 8rpx;
	margin-bottom: 30rpx;
	box-shadow: 0 10rpx 8rpx rgba($color: #878889, $alpha: 0.1);
	width: 100%;
	display: flex;
	padding: 30rpx;
	flex-direction: column;
	background-color: #ffffff;

	.row {
		display: flex;
		padding: 30rpx 0 20rpx;
		justify-content: space-around;
		align-items: center;

		.grid {
			width: 20%;
			display: flex;
			flex-direction: column;
			font-size: $font-size-sm;
			color: $text-color-assist;
			align-items: center;

			image {
				width: 80rpx;
				height: 80rpx;
				margin-bottom: 10rpx;
			}
		}
	}
}

.banner {
	width: 100%;
	border-radius: 8rpx;
	margin-bottom: 30rpx;
}

.service-box {
	width: 100%;
	background-color: #ffffff;
	padding: 32rpx 30rpx 10rpx;
	box-shadow: $box-shadow;

	.row {
		display: flex;
		flex-wrap: wrap;
		color: $text-color-assist;
		font-size: $font-size-sm;
		padding-bottom: -40rpx;

		.grid {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			margin-bottom: 40rpx;
			width: 25%;
			position: relative;

			.image {
				image {
					width: 80rpx;
					height: 80rpx;
					margin-bottom: 20rpx;
				}
			}

			.new-badage {
				width: 40rpx;
				height: 40rpx;
				position: absolute;
				top: 0;
				right: 30rpx;
			}
		}
	}
}

.contact {
	width: 100%;
	position: absolute;
	height: 70px;
	width:70px;
	left: 10px;
	top: 7px;
	z-index: 2;
	background: transparent !important;

	&::after {
		border: 0;
	}
}
</style>
